package my.jwds.p385;

import java.util.ArrayList;
import java.util.List;

public class Deserialize {

    public static void main(String[] args) {

        Solution obj = new Solution();
        long start = System.currentTimeMillis();
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    static class Solution {

        public NestedInteger deserialize(String s) {
            index =0;
            return process(s);
        }

        int index;
        NestedInteger process(String s){

            if (s.charAt(index) == '['){
                index++;
                NestedInteger ni = new NestedInteger();
                ni.getList().addAll(processNestedList(s)) ;
                return ni;
            }else{
                int res =0, c=1;
                for (int i = index; i < s.length(); i++) {
                    if (s.charAt(i) == '-'){
                        c = -1;
                    }else if (s.charAt(i) >='0' && s.charAt(i) <= '9'){
                        res = res *10 + (s.charAt(i) - '0');
                    }else{
                        index = i-1;
                        break;
                    }
                }
                return new NestedInteger(c*res);
            }
        }

        List<NestedInteger> processNestedList(String s){
            List<NestedInteger> list = new ArrayList<>();
            for (; index < s.length(); index++) {
                if (s.charAt(index) == ',')continue;
                if (s.charAt(index) == ']')break;
                list.add(process(s));
            }
            return list;
        }



    }



}